from django.contrib.gis.gdal import *
from location.models import Mine, LocationType, Source

ds = DataSource('data/mineplant.shp')
lyr = ds[0]

# fields: ['id', 'COMMODITY', 'SITE_NAME', 'COMPANY_NA', 'STATE_LOCA', 'COUNTY', 'LATITUDE', 'LONGITUDE', 'PLANT_MIN']

mtype = LocationType.objects.get(name='Mine')
msource = Source.objects.get(name='USGS Mine Database')
counter = 0
for feat in lyr:
    mine = Mine()
    mine.type = mtype
    mine.source = msource
    mine.name = feat.get('SITE_NAME')
    mine.xid = feat.get('id')
    mine.commodity = feat.get('COMMODITY')
    mine.company = feat.get('COMPANY_NA')
    mine.county = feat.get('COUNTY')
    mine.plant_min = feat.get('PLANT_MIN')
    mine.coordinates = 'POINT(%s %s)' % (feat.geom.x, feat.geom.y)
    mine.save()
    counter += 1

print "Saved %s records" % (counter)
